import types from './types'
import axios from 'axios'
import Rest from '@/service/rest'
// import lodash from 'lodash'

export default {
    /**
   * 获取列表
   */
  getImages ({commit}, params) {
    let url = '/image'
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params
      })
      .then(res => {
        let data = res.data
        commit(types.SET_IMAGES, data)
        resolve(data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  listImages ({commit}, params) {
    let url = '/list'
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params
      })
      .then(res => {
        commit(types.SET_IMAGE_LIST, res.data)
        resolve(res.data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 获取详情
   */
  getImage ({commit}, {id}) {
    let url = '/image/' + id
    return new Promise((resolve, reject) => {
      axios.get(url, {
      })
      .then(res => {
        commit(types.SET_IMAGE, res.data)
        resolve(res.data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 新增
   */
  postImage ({commit}, image) {
    let url = '/image'
    return new Promise((resolve, reject) => {
      axios.post(url, image, {})
      .then(res => {
        let item = res.data
        commit(types.ADD_IMAGE, item)
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 编辑
   */
  putImage ({commit}, image) {
    let url = '/image/' + image.id
    return new Promise((resolve, reject) => {
      axios.put(url, image, {})
      .then(res => {
        let item = res.data
        commit(types.UPDATE_IMAGE, item)
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 删除
   */
  deleteImage ({commit}, {id}) {
    let url = '/image/' + id
    return new Promise((resolve, reject) => {
      axios.delete(url)
      .then(res => {
        commit(types.DELETE_IMAGE, id)
        resolve(id)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  }

}
